import request from '../utils/request';

/**
 * 获取卖家产品列表
 */
export function getSellerProducts() {
  console.log('调用获取卖家产品列表API');
  return request({
    url: '/api/seller/product/my',
    method: 'get'
  }).then(response => {
    console.log('获取卖家产品列表成功:', response);
    return response;
  }).catch(error => {
    console.error('获取卖家产品列表失败:', error);
    throw error;
  });
}

/**
 * 上传产品图片
 */
export function uploadProductImage(file) {
  console.log('调用上传产品图片API');
  const formData = new FormData();
  formData.append('file', file);
  
  return request({
    url: '/api/seller/product/upload/image',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }).then(response => {
    console.log('上传产品图片成功:', response);
    return response;
  }).catch(error => {
    console.error('上传产品图片失败:', error);
    throw error;
  });
}

/**
 * 创建产品
 */
export function createProduct(data) {
  console.log('调用创建商品API, 数据:', data);
  return request({
    url: '/api/seller/product',
    method: 'post',
    data
  }).then(response => {
    console.log('创建商品API成功响应:', response);
    return response;
  }).catch(error => {
    console.error('创建商品API失败:', error);
    throw error;
  });
}

/**
 * 更新产品
 */
export function updateProduct(data) {
  console.log('调用更新商品API, 数据:', data);
  return request({
    url: '/api/seller/product',
    method: 'put',
    data
  }).then(response => {
    console.log('更新商品API成功响应:', response);
    return response;
  }).catch(error => {
    console.error('更新商品API失败:', error);
    throw error;
  });
}

/**
 * 删除产品
 */
export function deleteProduct(id) {
  console.log('调用删除商品API, ID:', id);
  return request({
    url: `/api/seller/product/${id}`,
    method: 'delete'
  }).then(response => {
    console.log('删除商品API成功响应:', response);
    return response;
  }).catch(error => {
    console.error('删除商品API失败:', error);
    throw error;
  });
}

/**
 * 批量删除产品
 */
export function batchDeleteProducts(ids) {
  console.log('调用批量删除商品API, IDs:', ids);
  return request({
    url: `/api/seller/product/batch-delete`,
    method: 'post',
    data: ids
  }).then(response => {
    console.log('批量删除商品API成功响应:', response);
    return response;
  }).catch(error => {
    console.error('批量删除商品API失败:', error);
    throw error;
  });
}

/**
 * 更新产品状态
 */
export function updateProductStatus(id, status) {
  console.log('调用更新商品状态API, ID:', id, '状态:', status);
  return request({
    url: `/api/seller/product/${id}/status/${status}`,
    method: 'put'
  }).then(response => {
    console.log('更新商品状态API成功响应:', response);
    return response;
  }).catch(error => {
    console.error('更新商品状态API失败:', error);
    throw error;
  });
}

/**
 * 批量更新产品状态
 */
export function updateProductsStatus(productIds, status) {
  console.log('调用批量更新商品状态API, IDs:', productIds, '状态:', status);
  return request({
    url: `/api/seller/product/batch-status`,
    method: 'post',
    data: { productIds, status }
  }).then(response => {
    console.log('批量更新商品状态API成功响应:', response);
    return response;
  }).catch(error => {
    console.error('批量更新商品状态API失败:', error);
    throw error;
  });
}

/**
 * 获取卖家订单列表
 */
export function getSellerOrders() {
  console.log('调用获取卖家订单列表API');
  return request({
    url: '/api/order/my/sell',
    method: 'get'
  }).then(response => {
    console.log('获取卖家订单列表成功:', response);
    return response;
  }).catch(error => {
    console.error('获取卖家订单列表失败:', error);
    throw error;
  });
}

/**
 * 发货
 */
export function shipOrder(data) {
  return request({
    url: `/api/order/${data.orderId}/ship`,
    method: 'put',
    data
  });
}

/**
 * 批量发货
 */
export function batchShipOrders(orderIds) {
  return request({
    url: '/api/order/batch/ship',
    method: 'post',
    data: { orderIds }
  });
}

/**
 * 取消订单
 */
export function cancelOrder(id) {
  return request({
    url: `/api/order/cancel/${id}`,
    method: 'put'
  });
}

/**
 * 退款
 */
export function refundOrder(id) {
  return request({
    url: `/api/order/refund/${id}`,
    method: 'put'
  });
}

/**
 * 获取投诉列表
 */
export function getComplaints() {
  return request({
    url: '/api/complaint/my',
    method: 'get'
  });
}

/**
 * 获取投诉详情
 */
export function getComplaintDetail(id) {
  return request({
    url: `/api/complaint/${id}`,
    method: 'get'
  });
}

/**
 * 回复投诉
 */
export function replyComplaint(id, content) {
  return request({
    url: `/api/complaint/${id}/reply`,
    method: 'put',
    data: { content }
  });
}

/**
 * 解决投诉
 */
export function resolveComplaint(id, solution) {
  return request({
    url: `/api/complaint/${id}/resolve`,
    method: 'put',
    data: { solution }
  });
}

/**
 * 关闭投诉
 */
export function closeComplaint(id) {
  return request({
    url: `/api/complaint/${id}/close`,
    method: 'put'
  });
} 